﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using SixApp.Entities;
using SixApp.Core;
using SixApp.Data;

namespace SixApp.Repositories
{
    public class HistoryRepository : BaseRepository
    {
        //public EntityList<History> GetAllHistories()
        //{
        //    var histories = new EntityList<History>();

        //    string sql = "select * from history order by id desc";

        //    DataTable dt = AccessHelper.GetDataTable(sql, null);

        //    histories.Load(dt);

        //    return histories;
        //}

        public DataTable GetAllHistories()
        {
            string sql = "select * from history order by id desc";

            DataTable dt = DbContext.GetDataTable(sql, null);

            return dt;
        }

        public void SaveOrUpdate(History history)
        {
            StringBuilder sbSql = new StringBuilder();
            Dictionary<string, object> args = new Dictionary<string, object>();

            if (history.Id > 0)
            {
                sbSql.Append(" update history set nianfen = @nianfen, qishu = @qishu, pinyi = @pinyi, piner = @piner, pinsan = @pinsan ")
                        .Append(", pinshi = @pinshi, pinwu = @pinwu, pinliu = @pinliu, tema = @tema, shengxiao = @shengxiao, bolu = @bolu ")
                        .Append(", dangshuang = @dangshuang, wuhang = @wuhang, jiaye = @jiaye, daxiao = @daxiao, tetou = @tetou ")
                        .Append(", weishu = @weishu, menshu = @menshu, duanwei = @duanwei, tmfw = @tmfw, hds = @hds, zhds = @zhds ")
                        .Append(" where id = " + history.Id);
                //args.Add("id", history.Id);
            }
            else
            {
                sbSql.Append("insert into history(nianfen, qishu, pinyi, piner, pinsan, pinshi, pinwu, pinliu, tema, shengxiao, bolu ")
                        .Append(", dangshuang, wuhang, jiaye, daxiao, tetou, weishu, menshu, duanwei, tmfw, hds, zhds) values ( ")
                        .Append("@nianfen, @qishu, @pinyi, @piner, @pinsan, @pinshi, @pinwu, @pinliu, @tema, @shengxiao, @bolu ")
                        .Append(",@dangshuang , @wuhang, @jiaye, @daxiao, @tetou, @weishu, @menshu, @duanwei, @tmfw, @hds, @zhds)");
            }

            args.Add("nianfen", history.nianfen);
            args.Add("qishu", history.qishu);
            args.Add("pinyi", history.pinyi);
            args.Add("piner", history.piner);
            args.Add("pinsan", history.pinsan);
            args.Add("pinshi", history.pinshi);
            args.Add("pinwu", history.pinwu);
            args.Add("pinliu", history.pinliu);
            args.Add("tema", history.tema);
            args.Add("shengxiao", history.shengxiao);
            args.Add("bolu", history.bolu);
            args.Add("dangshuang", history.dangshuang);
            args.Add("wuhang", history.wuhang);
            args.Add("jiaye", history.jiaye);
            args.Add("daxiao", history.daxiao);
            args.Add("tetou", history.tetou);
            args.Add("weishu", history.weishu);
            args.Add("menshu", history.menshu);
            args.Add("duanwei", history.duanwei);
            args.Add("tmfw", history.tmfw);
            args.Add("hds", history.hds);
            args.Add("zhds", history.zhds);

            DbContext.Execute(sbSql.ToString(), args);
        }

        public void DeleteHistory(int historyId)
        {
            string sql = "delete from history where id = @id";
            Dictionary<string, object> args = new Dictionary<string, object>();

            args.Add("id", historyId);

            DbContext.Execute(sql, args);

        }
    }
}
